﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace Akronus.TaskScheduler
{
    public static class XmlAttributeCollectionExtensions
    {
        /// <summary>
        /// Gets the attribute value. If the attribute is not found in the collection it throws an exception.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <returns>The value of attribute.</returns>
        public static string GetAttributeValue(this XmlAttributeCollection attributes, string attributeName)
        {
            if (attributes[attributeName] == null || string.IsNullOrEmpty(attributes[attributeName].Value))
                throw new ConfigurationErrorsException(string.Format("The '{0}' attribute was not found for the task.", attributeName));
            return attributes[attributeName].Value;
        }

        /// <summary>
        /// Gets the attribute value. If the attribute is not found in the collection it return the <paramref name="defaultValue"/>.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The value of attribute.</returns>
        public static string GetAttributeValue(this XmlAttributeCollection attributes, string attributeName, string defaultValue)
        {
            if (attributes[attributeName] == null || string.IsNullOrEmpty(attributes[attributeName].Value))
                return defaultValue;
            return attributes[attributeName].Value;
        }

    }
}
